package com.zlfund.cat.zjgsmk;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.zlfund.cat.zjgsmk.adapter.ViewpagerAdapter;
import com.zlfund.cat.zjgsmk.constant.Constants;
import com.zlfund.cat.zjgsmk.ui.base.BaseActivity;
import com.zlfund.cat.zjgsmk.ui.home.MainActivity;
import com.zlfund.cat.zjgsmk.util.DensityUtils;
import com.zlfund.cat.zjgsmk.util.GlideUtil;
import com.zlfund.cat.zjgsmk.util.SpUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class LeadActivity extends BaseActivity {

    @BindView(R.id.vp)
    ViewPager mVp;
    @BindView(R.id.ll_dot_container)
    LinearLayout mLlDotContainer;

    private List<ImageView> mDots;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);

            if (SpUtils.getStringFromGlobal(mContext, SpUtils.VERSION).equals(packageInfo.versionName)) {
                startActivity(new Intent(mContext,MainActivity.class));
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                finish();
            } else {
                SpUtils.putStringToGlobal(mContext, SpUtils.VERSION, packageInfo.versionName);
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    public ImageView createImageView(int resId) {
        ImageView iv = new ImageView(mContext);
        iv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
        GlideUtil.loadRes(mContext, resId, iv);
        return iv;
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_lead);
    }

    @Override
    protected void setListener() {
        mVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                changeDot(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void initData() {
        List<View> viewList = new ArrayList<>();
        mDots = new ArrayList<>();
        int[] resIds = new int[]{R.mipmap.welcome1, R.mipmap.welcome2, R.mipmap.welcome3, R.mipmap.welcome4};
        for (int i = 0; i < 4; i++) {
            viewList.add(createImageView(resIds[i]));
        }
        ViewpagerAdapter adapter = new ViewpagerAdapter(viewList);
        mVp.setAdapter(adapter);
        viewList.get(3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(mContext, MainActivity.class));
                finish();
            }
        });
        for (int i = 0; i < 4; i++) {
            ImageView iv = new ImageView(mContext);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(DensityUtils.dp2px(mContext, 5f), DensityUtils.dp2px(mContext, 5f));
            params.setMargins(getDP(8),getDP(8),getDP(8),getDP(8));
            iv.setLayoutParams(params);
            iv.setImageResource(R.drawable.shape_dot_normal);
            mLlDotContainer.addView(iv);
            mDots.add(iv);
        }
        changeDot(0);
    }

    private int getDP(float f){
        return DensityUtils.dp2px(mContext,f);
    }

    private void changeDot(int index){
        for (int i = 0; i < mDots.size(); i++) {
            if (i != index){
                mDots.get(i).setImageResource(R.drawable.shape_dot_normal);
            }else {
                mDots.get(i).setImageResource(R.drawable.shape_dot_checked);
            }
        }
    }
}
